const { Controller } = require("egg");

class CategoryController extends Controller {
  // 新增类目
  async addCategory() {
    const { ctx, service } = this;
    const { categoryName, icon } = ctx.request.body;
    ctx.validate({
      categoryName: { type: "nullValue", fieldName: "类别" },
      icon: { type: "nullValue", fieldName: "图标" },
    });

    const res = await service.category.addCategory(categoryName, icon);
    ctx.send(res);
  }
  // 获取类目列表
  async getCategory() {
    const { ctx, service } = this;
    const { page, size } = ctx.query;
    const res = await service.category.getCategory(page, size);
    ctx.send(res);
  }
  // 删除类目
  async delCategory() {
    const { ctx } = this;
    const { id } = ctx.params;
    console.log("我是id:", id);
    await ctx.model.Category.deleteOne({ _id: id });
    ctx.send({ msg: "成功" });
  }
  // 获取所有分类
  async getAllCategory() {
    const { ctx } = this;
    const res = await ctx.model.Category.find(
      {},
      { _id: true, categoryName: true }
    );
    ctx.send({ data: res });
  }
  //   获取分类及对应下的商品
  async getCategoryGoods() {
    const { ctx, service } = this;
    const res = await service.category.getCategoryGoods();
    ctx.send(res);
  }
}
module.exports = CategoryController;
